package cn.itsource.servlet;

import cn.itsource.domain.Student;
import cn.itsource.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/stu",loadOnStartup = 1)
public class StudentServlet  extends HttpServlet {
    StudentServiceImpl stuImpl = new StudentServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String type = req.getParameter("type");
        if (type.equals("login")){
            login(req, resp);
        }else if (type.equals("add")){
            saveStudent(req, resp);
        }else if (type.equals("del")) {
            deleteStudent(req, resp);
        }else if (type.equals("queryId")) {
            queryById(req, resp);
        }else if (type.equals("update")) {
            updateStudent(req, resp);
        }
    }
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Map<String, String[]> map = req.getParameterMap();
            Student stu = new Student();
            BeanUtils.copyProperties(stu,map);
            if (stu.getStuName().equals("admin") && stu.getStuAge().equals("21")){
                queryAll(req, resp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    protected void queryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> list = stuImpl.queryAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/stujsp/show.jsp").forward(req,resp);
    }
    protected void saveStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student stu = new Student("李四","11","女");
        boolean b = stuImpl.saveStudent(stu);
        if (b) {
            queryAll(req, resp);
        }else {
            System.out.println("学生添加失败");
        }
    }
    protected void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        boolean b = stuImpl.deleteStudent(id);
        System.out.println(123);
        if (b){
            queryAll(req, resp);
        }else {
            System.out.println("删除学生失败");
        }
    }
    protected void queryById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        Student stu = stuImpl.queryById(id);
        System.out.println(stu);
        if (stu!=null){
            req.setAttribute("stu",stu);
            req.getRequestDispatcher("/stujsp/edit.jsp").forward(req,resp);
        }
    }
    protected void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       try {
           Map<String, String[]> map = req.getParameterMap();
           Student stu = new Student();
           BeanUtils.copyProperties(stu,map);
           boolean b = stuImpl.updateStudent(stu);
           if (b) {
               queryAll(req, resp);
           }else {
               System.out.println("修改学生失败");
           }
       }catch (Exception e){
           e.printStackTrace();
       }

    }
}
